/*
 * ArticleCommentFragment.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.comment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.recyclertype.ArticleComment;
import com.yidiandao.business.ui.widget.dialog.CommentDialog;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.ArticleCommentProvider;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 发现-评论-View
 */
public class ArticleCommentFragment extends SuperBaseFragment implements ArticleCommentContract.View, ArticleCommentProvider.OnLikeClickListener {

    private ArticleCommentContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private RelativeLayout mBottomLayout;

    private RecyclerView mRecyclerView;

    private PtrClassicFrameLayout mPtrFrame;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private MultiTypeLoadMoreAdapter mAdapter;

    public static ArticleCommentFragment getInstance() {
        ArticleCommentFragment fragment = new ArticleCommentFragment();
        return fragment;
    }

    @Override
    public void setPresenter(ArticleCommentContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ArticleCommentFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mBottomLayout = (RelativeLayout) mRootView.findViewById(R.id.bottom_layout);
        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mLoadMoreContainer = (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);

        initRecyclerViews();
        initListeners();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });

        mBottomLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showArticleCommentDialog();
            }
        });

        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // 下拉刷新
                mPresenter.loadComments(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }
        });

        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                // 加载更多
                mPresenter.loadComments(false);
            }
        });
    }

    @Override
    public void showArticleCommentDialog() {
        final CommentDialog dialog = new CommentDialog(mActivity);
        dialog.show();
        dialog.setOnSubmitListener(new CommentDialog.OnSubmitListener() {
            @Override
            public void onSubmit(String text) {
                if (TextUtils.isEmpty(text)) {
                    showToast(getString(R.string.enter_comment_please));
                    return;
                }
                dialog.dismiss();
                mPresenter.submitComment(text);
            }
        });
    }

    private void initRecyclerViews() {
        mAdapter = new MultiTypeLoadMoreAdapter();
        mAdapter.register(ArticleComment.class, new ArticleCommentProvider(mActivity, this));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mLoadMoreContainer.setRecyclerViewAdapter(mAdapter);
    }

    @Override
    public void setItems(List<?> items) {
        mAdapter.setItems(items);
        notifyDataSetChanged();
    }

    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_article_comment;
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    public void refreshContent(int result) {
        if (result == SuperLoadingLayout.STATE_SUCCESS || result == SuperLoadingLayout.STATE_EMPTY) {
            mBottomLayout.setVisibility(View.VISIBLE);
        } else {
            mBottomLayout.setVisibility(View.GONE);
        }
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public void onLikeClick(ArticleComment articleComment) {
        mPresenter.likeComment(articleComment);
    }
}
